package com.multithreading.chart2.instructionReordering;

/**
 * @author chenqb
 * @version 1.0
 * @description: 演示指令重排序
 * @date 2021/8/3 10:41
 */
public class InstructionReordering {
    private static int num =0;
    private static volatile boolean ready=false;

    public static void main(String[] args) throws InterruptedException {
        ReadThread rt=new ReadThread();
        rt.start();

        WriteThread wt=new WriteThread();
        wt.start();

        Thread.sleep(10);
        rt.interrupt();
        System.out.println("main exit");


    }

    public static class ReadThread extends Thread{
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()){
                if (ready){//(1)
                    System.out.println(num+num);//(2)
                }
                System.out.println("read thread....");
            }
        }
    }

    public static class WriteThread extends Thread{
        @Override
        public void run() {
            num=2;//(3)
            ready=true;//(4)
            System.out.println("writeThread set over...");
        }
    }



}
